<template>
  <Form class="xtx-form" ref="uForm" :validation-schema="mySchema" autocomplete="off" v-slot="{errors}">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field name="account" class="input" v-model="form.account" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error" v-if="errors.account">{{errors.account}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field name="mobile" class="input" v-model="form.mobile" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error" v-if="errors.mobile">{{errors.mobile}}</div>

    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field name="code" class="input" v-model="form.code" type="text" placeholder="请输入验证码" />
        <span class="code" @click="send()">发送验证码</span>
      </div>
      <div class="error" v-if="errors.code">{{errors.code}}</div>

    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field name="password" class="input" v-model="form.password" type="password" placeholder="请输入密码" />
      </div>
      <div class="error" v-if="errors.password">{{errors.password}}</div>

    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field name="rePassword" class="input" v-model="form.rePassword" type="password" placeholder="请确认密码" />
      </div>
      <div class="error" v-if="errors.rePassword">{{errors.rePassword}}</div>

    </div>
    <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import { reactive, ref } from 'vue-demi'
import { useIntervalFn } from '@vueuse/shared'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import Message from '@/components/library/message'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'

export default {
  name: 'CallbackPatch',
  components: {
    Form, Field
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const store = useStore()
    const route = useRoute()
    const router = useRouter()
    // 表单数据
    const form = reactive({
      account: 'abc789',
      mobile: '13888866666',
      password: '123456',
      code: '123456',
      rePassword: '123456'
    })
    // 表单校验
    const mySchema = {
      account: schema.accountApi,
      mobile: schema.mobile,
      password: schema.password,
      code: schema.code,
      rePassword: schema.rePassword
    }
    // 发送验证码
    // 倒计时定时器
    const time = ref(0)
    const { pause, resume } = useIntervalFn(() => {
      time.value--
      if (time.value <= 0) {
        pause() // 清除定时器
      }
    }, 1000, false)
    const uForm = ref(null)
    const send = async () => {
      const valid = schema.mobile(form.mobile)
      if (valid === true) {
        if (time.value > 0) return
        await userQQPatchCode(form.mobile).then(res => {
          Message({ type: 'success', text: '发送成功' })
          time.value = 60
          resume() // 开启倒计时
        }).catch(e => {
          Message({ type: 'error', text: e.response.data.message || '发送失败' })
        })
      } else {
        uForm.value.setFieldError('mobile', valid)
      }
    }
    const submit = () => {
      const valid = uForm.value.validate()
      if (valid) {
        userQQPatchLogin({ unionId: props.unionId, ...form }).then(res => {
          store.commit('user/setUser', res.result)
          store.dispatch('cart/merageCart').then(() => {
            router.push(route.query.redirectUrl || '/')
            Message({ type: 'success', text: '登录成功' })
          })
        }).catch(e => {
          Message({ type: 'error', text: e.response.data.message || '登录失败' })
        })
      }
    }
    return { mySchema, form, send, uForm, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
